/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package managedbean.SLC;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.model.SelectItem;
import merlion.common.entity.GlobalVariable;
import merlion.common.session.stateless.GlobalVariableSessionBeanLocal;
import merlion.common.util.Consts;
import merlion.crm.slcm.pojo.PriceAnalysis;
import merlion.crm.slcm.session.SLCSessionBeanLocal;
import merlion.mrp.pmm.session.ProductManagementSessionLocal;
import merlion.mrp.ppm.entity.Product;
import org.primefaces.context.RequestContext;
import org.primefaces.event.SlideEndEvent;

/**
 *
 * @author Zhu Xin
 */
@ManagedBean(name = "ppManagerBean")
@ViewScoped
public class ProductPriceManagerBean {

    @EJB
    private SLCSessionBeanLocal slcSessionBean;
    @EJB
    private ProductManagementSessionLocal pmSessionBean;
    @EJB
    private GlobalVariableSessionBeanLocal gvSessionBean;
    private Product product;
    private List<Product> products;
    private SelectItem[] productTypeOptions;
    private Product selectedProduct;
    private Double minMemberDiscount;
    private Double maxMemberDiscount;
    private Double memberDiscount;
    private Double orderDiscount;
    private Double minOrderDiscount;
    private Double maxOrderDiscount;
    private Double specialDiscount;
    private GlobalVariable tax;
    private GlobalVariable specialDiscountRange;

    /** Creates a new instance of ProductPriceManagerBean */
    public ProductPriceManagerBean() {
        product = new Product();
        products = new ArrayList<Product>();
        selectedProduct = new Product();
        memberDiscount = 0d;
        orderDiscount = 0d;
        specialDiscount = 0d;
    }

    @PostConstruct
    public void init() {
        setProducts(slcSessionBean.getAllProduct());
        tax = gvSessionBean.getGlobalVariableByKey(Consts.GLOBAL_TAX_RATE);
        specialDiscountRange = gvSessionBean.getGlobalVariableByKey(Consts.GLOBAL_SPECIAL_DISCOUNT);
        minMemberDiscount = slcSessionBean.getMinimumMemberDiscountRate() * 100;
        maxMemberDiscount = slcSessionBean.getMaximumMemberDiscountRate() * 100;
        minOrderDiscount = slcSessionBean.getMinimumOrderDiscountRate() * 100;
        maxOrderDiscount = slcSessionBean.getMaximumOrderDiscountRate()* 100;
        System.out.println("" + minMemberDiscount + " " + maxMemberDiscount + " " + minOrderDiscount + " " + maxOrderDiscount);
    }
    
    public void onSlideEnd(SlideEndEvent event) {  
        System.out.println("slide end"); 
    }
    
    public void updateRates(ActionEvent event){
        tax = gvSessionBean.updateGlobalVariable(tax);
        specialDiscountRange = gvSessionBean.updateGlobalVariable(specialDiscountRange);
        
        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Rates", "have been updated."));
    }

    public Collection<PriceAnalysis> getPriceAnalysis() {

        ArrayList<Product> products = new ArrayList<Product>(slcSessionBean.getAllProduct());
        ArrayList<PriceAnalysis> prices = new ArrayList<PriceAnalysis>();
        
        for (Product p : products) {
            PriceAnalysis pa = new PriceAnalysis(p, memberDiscount/100d, orderDiscount/100d, specialDiscount/100d, tax.getDoubleValue());
            prices.add(pa);
        }
        
        return prices;

    }

    public Double getSpecialDiscount() {
        return specialDiscount;
    }

    public void setSpecialDiscount(Double specialDiscount) {
        this.specialDiscount = specialDiscount;
    }

    public GlobalVariable getSpecialDiscountRange() {
        return specialDiscountRange;
    }

    public void setSpecialDiscountRange(GlobalVariable specialDiscountRange) {
        this.specialDiscountRange = specialDiscountRange;
    }

    public GlobalVariable getTax() {
        return tax;
    }

    public void setTax(GlobalVariable tax) {
        this.tax = tax;
    }

    public Double getMemberDiscount() {
        return memberDiscount;
    }

    public void setMemberDiscount(Double memberDiscount) {
        this.memberDiscount = memberDiscount;
    }

    public Double getOrderDiscount() {
        return orderDiscount;
    }

    public void setOrderDiscount(Double orderDiscount) {
        this.orderDiscount = orderDiscount;
    }

    public Double getMaxMemberDiscount() {
        return maxMemberDiscount;
    }

    public void setMaxMemberDiscount(Double maxMemberDiscount) {
        this.maxMemberDiscount = maxMemberDiscount;
    }

    public Double getMaxOrderDiscount() {
        return maxOrderDiscount;
    }

    public void setMaxOrderDiscount(Double maxOrderDiscount) {
        this.maxOrderDiscount = maxOrderDiscount;
    }

    public Double getMinMemberDiscount() {
        return minMemberDiscount;
    }

    public void setMinMemberDiscount(Double minMemberDiscount) {
        this.minMemberDiscount = minMemberDiscount;
    }

    public Double getMinOrderDiscount() {
        return minOrderDiscount;
    }

    public void setMinOrderDiscount(Double minOrderDiscount) {
        this.minOrderDiscount = minOrderDiscount;
    }

    public Product getProduct() {
        return product;
    }

    public void setProduct(Product product) {
        this.product = product;
    }

    public SLCSessionBeanLocal getSlcSessionBean() {
        return slcSessionBean;
    }

    public void setSlcSessionBean(SLCSessionBeanLocal slcSessionBean) {
        this.slcSessionBean = slcSessionBean;
    }

    public List<Product> getProducts() {
        return products;
    }

    public void setProducts(List<Product> products) {
        this.products = products;
    }

    public SelectItem[] getProductTypeOptions() {
        return productTypeOptions;
    }

    public void setProductTypeOptions(SelectItem[] productTypeOptions) {
        this.productTypeOptions = productTypeOptions;
    }

    public Product getSelectedProduct() {
        return selectedProduct;
    }

    public void setSelectedProduct(Product selectedProduct) {
        this.selectedProduct = selectedProduct;
    }

    public SelectItem[] createFilterOptions() {
        SelectItem[] options = new SelectItem[products.size() + 1];

        options[0] = new SelectItem("", "Select");
        //System.out.print(options[0]);
        //System.out.print(products.size());
        for (int i = 0; i < products.size(); i++) {
            options[i + 1] = new SelectItem(products.get(i).getProductType(), products.get(i).getProductType());
            //System.out.print(options[i+1]);
        }

        productTypeOptions = options;
        return options;
    }

    public List<Product> viewAllProductPrices() {
        return products;
    }

    private void resetSession() {
        product = new Product();
    }

    public void updateBasePrice() {
        RequestContext context = RequestContext.getCurrentInstance();
        FacesMessage msg = null;
        boolean valid = true;

        //Base Price cannot be empty
        if (selectedProduct.getBasePrice() == null) {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_WARN, "Product Base Price", "cannot be empty."));
            valid = false;
        }
        //Base Price cannot be negative
        if (selectedProduct.getBasePrice() < 0) {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_WARN, "Product Base Price", "cannot be negative."));
            valid = false;
        }

        if (valid) {
            try {
                selectedProduct = pmSessionBean.updateProductBasePrice(selectedProduct);
                //return "updateProductPrice";

            } catch (Exception e) {
                // return "error";
            }
        }

        context.addCallbackParam("valid", valid);

    }

    public void updateProductPrice() {
        RequestContext context = RequestContext.getCurrentInstance();
        FacesMessage msg = null;
        boolean valid = true;

        //wsMarkup cannot be empty and cannot be less than 1
        if (selectedProduct.getWsMarkup() == null) {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_WARN, "Whole Sales Price Markup", "cannot be empty."));
            valid = false;

        } else if (selectedProduct.getWsMarkup() < 1) {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_WARN, "Whole Sales Price Markup", "cannot be less than 1."));
            valid = false;
        }

        //dsMarkup cannot be empty and cannot be less than 1
        if (selectedProduct.getDsMarkup() == null) {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_WARN, "Direct Sales Price Markup", "cannot be empty."));
            valid = false;
        } else if (selectedProduct.getDsMarkup() < 1) {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_WARN, "Direct Sales Price Markup", "cannot be less than 1."));
            valid = false;
        }

        if (valid) {
            try {
                selectedProduct = slcSessionBean.updateProductPrice(selectedProduct);
                //return "updateProductPrice";

            } catch (Exception e) {
                // return "error";
            }
        }

        //FacesContext.getCurrentInstance().addMessage(null, msg);
        context.addCallbackParam("valid", valid);


        /* if (i == 0) {
        resetSession();
        return "updateProductPrice";
        } else {
        //Validate
        /*if (!validate(product)) {
        return "updateProductPrice";
        }*/

        /* product = slcSessionBean.updateProductPrice(product);
        
        
        // Reset Manager Bean odg and product object after creating
        resetSession();
        
        //Forward to confirm page
        return "confirm";
        }*/
    }
    /*
    public String deleteProductPrice() {
    try {
    System.out.print(selectedProduct.getProductType());
    slcSessionBean.deleteProductPrice(selectedProduct);
    System.out.print(selectedProduct.getProductType());
    return "deletedProductPrice";
    
    } catch (Exception e) {
    return "error";
    }
    
    }
     */
}
